home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / PipedReader.java < prev    next >
Text File  |  1998-09-22  |  3KB  |  118 lines

  1. /*
  2.  * @(#)PipedReader.java    1.6 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17.  
  18. /**
  19.  * Piped character-input streams.
  20.  *
  21.  * @version     1.6, 98/07/01
  22.  * @author    Mark Reinhold
  23.  * @since    JDK1.1
  24.  */
  25.  
  26. public class PipedReader extends Reader {
  27.  
  28.     PipedInputStream byteSink;
  29.  
  30.     private byte buf[];        /* Conversion buffer */
  31.     private int leftOver = 0;
  32.  
  33.     /**
  34.      * Create a reader that is not yet connected to a piped writer.
  35.      */
  36.     public PipedReader() {
  37.     byteSink = new PipedInputStream();
  38.     lock = byteSink;
  39.     }
  40.  
  41.     /**
  42.      * Create a reader for the specified piped character-output stream.
  43.      */
  44.     public PipedReader(PipedWriter src) throws IOException {
  45.     this();
  46.     connect(src);
  47.     }
  48.  
  49.     /** Check to make sure that the stream has not been closed */
  50.     private void ensureOpen() throws IOException {
  51.     if (byteSink == null)
  52.         throw new IOException("Stream closed");
  53.     }
  54.  
  55.     /**
  56.      * Connect the specified piped writer to this reader.
  57.      *
  58.      * @exception  IOException  If this reader is already connected
  59.      */
  60.     public void connect(PipedWriter src) throws IOException {
  61.     synchronized (lock) {
  62.         ensureOpen();
  63.         src.connect(this);
  64.     }
  65.     }
  66.  
  67.     /**
  68.      * Read characters into a portion of an array.
  69.      *
  70.      * @param      cbuf  Destination buffer
  71.      * @param      off   Offset at which to start storing characters
  72.      * @param      len   Maximum number of characters to read
  73.      *
  74.      * @return     The number of characters read, or -1 if the end of the
  75.      *             stream has been reached
  76.      *
  77.      * @exception  IOException  If an I/O error occurs
  78.      */
  79.     public int read(char cbuf[], int off, int len) throws IOException {
  80.     synchronized (lock) {
  81.         ensureOpen();
  82.  
  83.         int blen = leftOver + len * 2;
  84.         if ((buf == null) || (buf.length < blen))
  85.         buf = new byte[blen];
  86.         int nb = byteSink.read(buf, leftOver, blen);
  87.         if (nb < 0)
  88.         return -1;
  89.         nb += leftOver;
  90.         for (int i = 0; i < nb; i += 2)
  91.         cbuf[i >> 1] = (char) (((buf[i] & 0xff) << 8)
  92.                        | (buf[i + 1] & 0xff));
  93.         if (nb % 2 != 0) {
  94.         buf[0] = buf[nb - 1];
  95.         leftOver = 1;
  96.         }
  97.         else
  98.         leftOver = 0;
  99.         return nb / 2;
  100.     }
  101.     }
  102.  
  103.     /**
  104.      * Close the stream.
  105.      *
  106.      * @exception  IOException  If an I/O error occurs
  107.      */
  108.     public void close() throws IOException {
  109.     synchronized (lock) {
  110.         if (byteSink == null)
  111.         return;
  112.         byteSink.close();
  113.         byteSink = null;
  114.     }
  115.     }
  116.  
  117. }
  118.